﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OrgUserPicker.aspx.cs" Inherits="Web.Views.SystemConfig.OrgUserPicker" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<base target="_self" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Cache-Control" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    <title>机构人员选择</title>
    <link href="../../App_Themes/NewPage/styles/tables.css" rel="Stylesheet" type="text/css" />
    <link href="../../App_Themes/NewPage/styles/layout.css" rel="Stylesheet" type="text/css" />
         <link type="text/css" href="../../App_Themes/NewPage/Styles/sexybuttons.css" rel="Stylesheet" />
    <script src="../../Easy_UI/jquery-1.6.min.js" type="text/javascript"></script>
    <link href="../../zTree3.4/css/zTreeStyle/zTreeStyle.css" rel="stylesheet"
        type="text/css" />
<script src="../../zTree3.4/jquery.ztree.all-3.4.js" type="text/javascript"></script>


</head>
<body>
    <form id="form1" runat="server">
    <div style=" margin-bottom: 10px; padding-right:20px; text-align: right;">
        <button type="button" class="sexybutton" runat="server" id="btnSelect"><span><span><span class="select">选择</span></span></span></button>
        <button type="button" class="sexybutton" runat="server" id="btnClose" ><span><span><span class="cancel">取消</span></span></span></button>
    </div>
    <div >
        <ul id="treeCompany" class="ztree" dnd="false">
        </ul>
    </div>
    <asp:HiddenField ID="H_mode" runat="server" />
            <asp:HiddenField ID="h_parentId" runat="server" />
                    <asp:HiddenField ID="h_oldUserId" runat="server" /> 
    </form>
    <script type="text/javascript">
        //加载公共通讯录Tree

        var toporgId = $("#h_parentId").val();
        var oldUserId = $("#h_oldUserId").val();
        var mode = $("#H_mode").val();
        //zTree设置
        //时间戳变量
        var timestamp = new Date().getTime(),
        //定义用户的顶级组织机构id(测试中默认为175)
       topOrgId = "100",
        //定义选择模式[默认为多选]（单选，多选，或者仅叶节点可选。对应的值分别为single，more，leaf）
       selectMode = (mode == "more" ? true : false),
        //定义数据过滤函数，以适应以前EasyUI的数据结构
       dataFilter = function () { },
        //定义根据节点id获取节点信息的函数，以适应以前的EasyUI的数据结构
       getTreeNode = function () { },
        //加载zTree的通用设置
       options = {},
        //初始化zTree的设置
       settings,
        //定义加载公司通讯录的函数
       loadCompanyTree = function () { };

        //数据过滤
        dataFilter = function (treeId, parentNode, childNodes) {
            var filterNodes = [];
            $.each(childNodes, function (index, node) {
                //过滤节点pid为空
                if (node !== undefined && node.pid !== null || node.pid !== "") {
                    filterNodes.push(node);
                }
            });
            $.each(filterNodes, function (index, node) {
                //不显示复选框
                if (node !== undefined && node.showCbx !== undefined && !node.showCbx) {
                    node.nocheck = "true";
                }
                //子节点的数据显示 +
                if (node !== undefined && node.state !== undefined && node.state === "open") {
                    node.open = true;
                }
                else {
                    node.isParent = true;
                }
                //如果节点ID为顶级节点，显示+并且展开
                if (node != undefined && node.id == topOrgId) {
                    node.open = true;
                }
                //子节点的图标样式
                //node.iconSkin = node.iconCls;
                if (node != undefined && node.iconCls != undefined) {
                    switch (node.iconCls) {
                        case "icon-user":
                            node.icon = "../../Easy_UI/themes/icons/people.png";
                            break;
                        case "icon-unit":
                            node.icon = "../../Easy_UI/themes/icons/icon_unit.png";
                            break;
                        case "icon-org":
                            node.icon = "../../Easy_UI/themes/icons/icon_org2.png";
                            break;
                        case "icon-group":
                            node.icon = "../../Easy_UI/themes/icons/Group.gif";
                            break;
                        case "icon-folder":
                            node.icon = "../../Easy_UI/themes/icons/folder.gif";
                            break;
                        case "icon-ad-user":
                            node.icon = "../../Easy_UI/themes/icons/icon-ad-user.png";
                            break;
                        case "icon-ad-pub":
                            node.icon = "../../Easy_UI/themes/icons/icon-ad-pub.png";
                            break;
                        default:
                            break;
                    }
                }
            });
            //                //处理UserID与OrgID重复问题,在UserID前加"u"，返回数据时需要特殊处理
            //                $.each(filterNodes, function (index, node) {
            //                    //修改UserId与OrgId重复数据，UserId前面加 "u",做特殊标识
            //                    if (node !== undefined && node.attributes !== undefined && node.attributes !== null && node.attributes.type !== undefined && node.attributes.type !== null && node.attributes.type === "user") {
            //                        node.id = "u" + node.id;
            //                    }
            //                });
            return filterNodes;
        };
        //获取节点,判断用户节点时需要+"u"判断
        function getTreeNode(treeid, id) {
            var treeObj, node, resultNode = null;
            treeObj = $.fn.zTree.getZTreeObj(treeid);
            nodes = treeObj.transformToArray(treeObj.getNodes());
            $.each(nodes, function (index, node) {
                var strId = id;
                if (node.attributes !== undefined && node.attributes !== null && node.attributes.type === "user") {
                    strId = "u" + id;
                }
                if (node !== undefined && node.id == strId) {
                    resultNode = node;
                    return;
                }
            });
            return resultNode;
        }

        settings = {
            async: {
                enable: true,
                url: "OrgUserHandle.ashx?CompanyAdress=yes&ShowOrgCheckbox=" + selectMode + "&timestamp=" + timestamp + "",
                autoParam: ["id=pid"],
                dataFilter: dataFilter
            },
            check: {
                enable: false,
                chkStyle: "checkbox",
                chkboxType: { "Y": "", "N": "" }
            },
            data: {
                key: {
                    name: "text"
                },
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pid",
                    rootPId: 0
                }
            },
            callback: {
                beforeCheck: null,
                beforeClick: null,
                beforeCollapse: null,
                beforeExpand: null,
                onCheck: null,
                onAsyncError: null,
                onAsyncSuccess: null,
                onCollapse: null,
                onExpand: null
            }
        };


        //加载公司通讯录
        loadCompanyTree = function () {
            //初始化公司通讯录tree
            //设置选择模式
            settings.check.enable = true;
            //设置远程数据地址(带参数)
            settings.async.url = "OrgUserHandle.ashx?CompanyAdress=yes&ShowOrgCheckbox=" + selectMode + "&timestamp=" + timestamp + "";
            //选中组织机构后，自动选中该节点的子节点的人,机构不选中
            settings.callback.onCheck = function (event, treeId, node) {
                if (node.attributes.type == "org") {
                    var zTreeObj = $.fn.zTree.getZTreeObj("treeCompany");
                    var nodes = zTreeObj.getNodesByParam("pid", node.id, null);
                    if (nodes.length == 0) {
                        zTreeObj.expandNode(node, true, false, true, true);
                        return;
                    }
                    //遍历子节点
                    $.each(nodes, function (i, item) {
                        //如果是人员，则选中状态和父节点一致
                        if (item.attributes.type == "user") {
                            item.checked = node.checked;
                            zTreeObj.updateNode(item);
                        }
                    });
                }
            };

            //节点展开事件
            settings.callback.onExpand = function (event, treeId, node) {
                var zTreeObj = $.fn.zTree.getZTreeObj("treeCompany");
                //查找出所有的子节点，如果有的话
                var nodes = zTreeObj.getNodesByParam('pid', node.id);
                $.each(nodes, function (i, item) {
                    if (item.attributes.type == "user") {
                        if (node.checked == true && item.checked == false) {
                            item.checked = true;
                            zTreeObj.updateNode(item);
                        }
                    }
                });
            };
            $.fn.zTree.init($("#treeCompany"), settings);
        };
        $(function () {
            //加载公司通讯录
            loadCompanyTree();
            //绑定选择人员事件
            if (!selectMode) {
                $("#btnSelect").bind("click", function () {
                    var comTree = $.fn.zTree.getZTreeObj("treeCompany");
                    var nodes = [];
                    nodes = comTree.getSelectedNodes();
                    if (nodes.length == 0) {
                        alert("没有选择人员");
                        return;
                    }
                    var data = new String();
                    data = "[";
                    var nodeData = nodes[0];
                    if (nodeData.attributes.type == 'user') {
                        data += "{\"nodeId\":\"" + nodeData.id + "\",\"nodeData\":\"" + nodeData.text + "\"}";
                    }
                    data += "]";
                    window.top.returnValue = data;
                    window.top.close();
                });
            }
            else {
                $("#btnSelect").bind("click", function () {
                    //定义3个通讯录tree对象
                    var comTree = $.fn.zTree.getZTreeObj("treeCompany");
                    var nodes = [];
                    nodes = comTree.getCheckedNodes();
                    var data = new String();
                    data = "[";
                    if (nodes.length > 0) {
                        for (var i = 0; i < nodes.length; i++) {
                            var nodeData = nodes[i];
                            if (nodeData.attributes.type == 'user') {
                                var nodeid = nodeData.id.substring(1, nodeData.id.length); ;
                                data += "{\"nodeId\":\"" + nodeid + "\",\"nodeData\":\"" + nodeData.text + "\"},";
                            }
                        }
                        if (data.length > 1) {
                            data = data.substring(0, data.length - 1);
                        }
                    }
                    data += "]";
                    window.top.returnValue = data;
                    window.top.close();
                });
            }
            $("#btnClose").bind("click", function () {
                window.top.close();
            });
        });

    </script>
</body>
</html>